package com.ikun.yummy.activitys.person;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.ikun.yummy.App;
import com.ikun.yummy.R;
import com.ikun.yummy.activitys.details.DetailActivity;
import com.ikun.yummy.adapter.CookListAdapter;
import com.ikun.yummy.bean.CookInfo;
import com.ikun.yummy.bean.FollowInfo;
import com.ikun.yummy.bean.UserInfo;
import com.ikun.yummy.manager.CookOpManager;
import com.ikun.yummy.manager.FollowOPManager;
import com.ikun.yummy.manager.LikeOpManager;
import com.ikun.yummy.manager.UserOpManager;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class PersonActivity extends AppCompatActivity {
    private ListView listView;
    private TextView username;
    private ImageView userimg;
    private UserOpManager userOpManager;
    private UserInfo personInfo;
    private UserInfo userInfo;
    private Button followBtn;
    private FollowOPManager followOPManager;
    private CookListAdapter adp;
    private List<CookInfo> cookList = new ArrayList<>();
    private CookOpManager cookOpManager;
    private LikeOpManager likeOpManager;
    private TextView cookcount;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person);
        initView();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
    }
    private void initView() {
        String myname = App.sp.getString("username", "null");
        if (myname.equals("null")){
            Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
        }else{
            listView=findViewById(R.id.person_list);
            username=findViewById(R.id.person_username);
            userimg=findViewById(R.id.person_userimg);
            followBtn=findViewById(R.id.person_follow);
            cookcount=findViewById(R.id.person_cookcount);

            userOpManager=new UserOpManager(this);
            followOPManager=new FollowOPManager(this);
            cookOpManager=new CookOpManager(this);
            likeOpManager=new LikeOpManager(this);

            personInfo=userOpManager.queryUser(getIntent().getStringExtra("username"));
            userInfo=userOpManager.queryUser(myname);

            initMethod();
        }
    }
    @SuppressLint("SetTextI18n")
    private void initMethod() {
        FollowInfo followInfo = followOPManager.queryFollowerSingle(userInfo.username,personInfo.username);
        if (followInfo!=null){
            followBtn.setText("取关");
        }else{
            followBtn.setText("关注");
        }
        followBtn.setOnClickListener(v -> {
            if (userInfo.username.equals(personInfo.username)){
                Toast.makeText(this, "不能关注自己哦", Toast.LENGTH_SHORT).show();
                return;
            }
            FollowInfo followInfo1 = followOPManager.queryFollowerSingle(userInfo.username,personInfo.username);
            if (followInfo1!=null){
                followBtn.setText("关注");
                Toast.makeText(this,"已取消关注",Toast.LENGTH_SHORT).show();
                followOPManager.delete(userInfo.username,personInfo.username);
            }else{
                followBtn.setText("取关");
                Toast.makeText(this,"已关注"+personInfo.username,Toast.LENGTH_SHORT).show();
                followOPManager.insertFollower(new FollowInfo("",userInfo.username,personInfo.username,1));
                followOPManager.insertFollowee(new FollowInfo("",personInfo.username,userInfo.username,1));
            }
        });
        username.setText(personInfo.username);
        File file=new File(personInfo.userimage);
        if (file.exists()){
            userimg.setImageBitmap(BitmapFactory.decodeFile(personInfo.userimage));
        }else{
            userimg.setImageResource(R.drawable.app_logo);
        }
        adp=new CookListAdapter(this, R.layout.item_cook_no_like, cookList,1,null);
        listView.setAdapter(adp);
        List<CookInfo> cooks = cookOpManager.queryByUsername(personInfo.username);
        if (cooks!=null){
            cookcount.setText("TA的菜谱("+cooks.size()+")");
            cookList.addAll(cooks);
            adp.notifyDataSetChanged();
        }
        listView.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent=new Intent(PersonActivity.this, DetailActivity.class);
            intent.putExtra("cookid", cookList.get(position).cookid);
            startActivity(intent);
        });
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
